package cn.newtest.common.dbf;

/**
 * <p>
 * Title: RiseReport
 * </p>
 * <p>
 * Description: RiseReport
 * </p>
 * <p>
 * Copyright: Copyright (c) 2007
 * </p>
 * <p>
 * Company: SZRiseSoft
 * </p>
 * 
 * @author WangChui(baiyibing@gmail.com)
 * @version 2.0 TODO
 * 
 */
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Formater0 implements IFormater {
	private static final Log log = LogFactory.getLog(Formater0.class);

	public Formater0(int specialOP) {
		this.specialOP = specialOP;
	}

	private int specialOP = 0;

	public ArrayList operate(StringBuffer str, int len, String rowSpliter,
			String spliter, JDBField[] jdbFields) {
		// TODO Auto-generated method stub

		log.debug("specialOP = 0");
		ArrayList alist = new ArrayList();
		String[] rows = StringUtils.split(str.toString(), rowSpliter);
		String[] data = null;
		Object[] tmp = null;
		for (int i = 0; i < rows.length; i++) {
			tmp = new Object[len];
			/*
			 * if(rows[i].endsWith(spliter)){ rows[i] = rows[i]+" "; }
			 */
			log.debug("rows[i]: " + rows[i]);
			data = StringUtils.split(rows[i], spliter);
			log.debug("data.length=" + data.length);
			log.debug("titles.length=" + len);
			log.debug("jdbFields.length=" + jdbFields.length);
			
			for (int j = 0; j < len; j++) {

				if (jdbFields[j].getType() == 'N') {
					if (data[j] == null)
						data[j] = "0";
					if (data[j].trim().equals(""))
						tmp[j] = new Integer("0");
					else {
						 
						try{
                            // 兼容小数。 
							tmp[j] = new Integer((int)Double.parseDouble(data[j])); 
						}catch(Exception ex)
						{
//							jdbFields[j].setType('C');
//							tmp[j]=(String)data[j];
						}
						
					}
				} else if (jdbFields[j].getType() == 'F') {
					if (data[j].trim().equals(""))
						tmp[j] = new Float("0");
					else
						tmp[j] = new Float(data[j]);
				} else if (jdbFields[j].getType() == 'B') {
					if (data[j].trim().equals(""))
						tmp[j] = new Double("0");
					else
						tmp[j] = new Double(data[j]);
				} else if (jdbFields[j].getType() == 'C') {
					int fieldLen = jdbFields[j].getLength();
					// if(fieldLen - 1 < data[j].length()){
					// tmp[j] = data[j].substring(0,fieldLen - 1);
					// 不明白李开始为什么要-1长度
					if (fieldLen < data[j].length()) {
						tmp[j] = data[j].substring(0, fieldLen);
					} else {
						tmp[j] = data[j];
					}
					
				} else if (jdbFields[j].getType() == 'D') {
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d");
					try {
						tmp[j] = sdf.parse(data[j]);// 跨过名称代码
					} catch (Exception ex) {
						ex.printStackTrace();
						tmp[j] = new Date();
					}
				} else if (jdbFields[j].getType() == 'L') {
					tmp[j] = new Boolean(data[j]);// 跨过名称代码
				} else {
					tmp[j] = data[j];// 跨过名称代码
				}

			}

			alist.add(tmp);
		}
		return alist;

	}

}
